#include "AmbientLightComponent.h"
#include "SceneSystem.h"

IEngineComponentRegistry ambientLightComponentRegistery("AmbientLightComponent", AmbientLightComponent::Deserialize);

AmbientLightComponent::AmbientLightComponent()
{
	this->color = D3DXVECTOR3(1,1,1);
	this->intensity = 1;
}

AmbientLightComponent::AmbientLightComponent(D3DXVECTOR3 color, float intensity)
{
	this->color = color;
	this->intensity = intensity;
}

AmbientLightComponent::~AmbientLightComponent(void)
{
}

float AmbientLightComponent::getIntensity()
{
	return intensity;
}

void AmbientLightComponent::setIntensity(float intensity)
{
	this->intensity = intensity;
}

D3DXVECTOR3 AmbientLightComponent::getColor()
{
	return color;
}

void AmbientLightComponent::setColor(D3DXVECTOR3 color)
{
	this->color = color;
}

void AmbientLightComponent::Serialize(EnginePointer<ISerializerWriteContext>& context, char* name)
{
	context->StartItem(name, "AmbientLightComponent", "1.0");
	context->SerializeProperty("componentId", this->componentId);
	context->SerializeProperty("intensity", intensity);
	context->SerializeProperty("colorR", color.x);
	context->SerializeProperty("colorG", color.y);
	context->SerializeProperty("colorB", color.z);
	context->EndItem(name);
}

IEngineComponent* AmbientLightComponent::Deserialize(EnginePointer<ISerializerReadContext>& context)
{
	AmbientLightComponent* ambientLight = new AmbientLightComponent();
	int index = 0;
	char* childName = context->ChildName(index);

	while (childName != NULL)
	{
		if (strcmp(childName, "componentId") == 0)
		{
			ambientLight->componentId = context->ChildValueAsULongLong(index);
		}
		else if (strcmp(childName, "intensity") == 0)
		{
			float intensity = context->ChildValueAsFloat(index);
			ambientLight->setIntensity(intensity);
		}
		else if(strcmp(childName, "colorR") == 0)
		{
			D3DXVECTOR3 color = ambientLight->getColor();
			color.x = context->ChildValueAsFloat(index);
			ambientLight->setColor(color);
		}
		else if(strcmp(childName, "colorG") == 0)
		{
			D3DXVECTOR3 color = ambientLight->getColor();
			color.y = context->ChildValueAsFloat(index);
			ambientLight->setColor(color);
		}
		else if(strcmp(childName, "colorB") == 0)
		{
			D3DXVECTOR3 color = ambientLight->getColor();
			color.z = context->ChildValueAsFloat(index);
			ambientLight->setColor(color);
		}
		childName = context->ChildName(++index);
	}
	
	return ambientLight;
}

GUID AmbientLightComponent::getSystemIdentifier()
{
	return SCENE_SYSTEM_GUID;
}

GUID AmbientLightComponent::getComponentIdentifier()
{
	return AMBIENT_LIGHT_COMPONENT_GUID;
}
